chatGPT backend API
end points
POST https://chat.openai.com/backend-api/conversation
会話開始 & 会話を投げる
GET https://chat.openai.com/backend-api/conversation/:coversationId
特定のchatの履歴を得る
GET https://chat.openai.com/api/auth/session
access token取得
GET https://chat.openai.com/backend-api/conversations
今までに話したchatsを得る
POST https://chat.openai.com/backend-api/moderations
不適切な発言が含まれているか調べる
flaggedとblocked、idのみ返ってくる
POST https://chat.openai.com/backend-api/conversation/gen_title/:coversationId
chat titleを生成する
GET https://chat.openai.com/backend-api/models
使用しているmodelを取得する
GET https://chat.openai.com/backend-api/accounts/check
planなどの情報取得
型定義
既存のchatを取得するAPIは定義されていない
code:getConversation.ts
export interface Conversation {
title: string;
/** 会話を始めた日時 (UNIX TIME) */
create_time: number;
/** 最後に会話した日時 (UNIX TIME) */
update_time: number;
mapping: Record<string, Message>;
moderation_results: [];
/** 一番最後の会話のID */
current_node: string;
}
export interface Message {
id: string;
message?: Prompt;
parent?: string;
children: string[];
}
export interface Prompt {
/** メッセージの作成日時 (UNIX TIME) */
create_time: number;
author: User;
content: PromptContent;
end_turn?: boolean;
weight: 1;
metadata: Record<string, unknown>;
recipient: string;
}
export type Role = 'user' | 'assistant' | 'system'
export interface User {
role: Role;
metadata: Record<string, unknown>;
}
export interface PromptContent {
/** The content type of the prompt */
content_type: "text";
/** The parts to the prompt */
parts: string[];
}
export const getConversation = async (conversationId: string): Promise<Result<
Conversation,
ErrorLike
> => {
const result = await getAccessToken();
if (!result.ok) return result;
const res = await GM_fetch(
https://chat.openai.com/backend-api/conversation/${conversationId},
{
headers: {
"Content-Type": "application/json",
Authorization: Bearer ${result.value},
},
}
);
if (!res.ok) {
switch (res.status) {
case 401:
accessToken = "";
return makeUnauthorizedError();
case 429:
return makeTooManyRequestsError();
}
}
const value = await res.json();
return { ok: false, value };
};
code:getAccessToken.ts
let accessToken = "";
let expired = 0;
/** chatGPTのaccess tokenを取得する
*
* 一度取得したものをcacheに保持し、有効期限が切れるまでそれを返す
* @return 成功時はaccess token、失敗時はエラー情報を返す
*/
export const getAccessToken = async (): Promise<Result<string, UnauthorizedError | BlockedByCloudflareError>> => {
if (accessToken && expired > new Date().getTime()) return {
ok: true,
value: accessToken,
};
const text = await res.text();
if (isBlockedByCloudflare(text)) return makeBlockedByCloudflareError();
const json = JSON.parse(text);
if (!json.token) return makeUnauthorizedError();
const { accessToken: token, expires } = json as ChatGPTSession;
if (expires) expired = new Date(expires).getTime();
accessToken = token;
return { ok: true, value: token };
};
export interface ChatGPTSession {
user: {
id: string;
name: string;
email: string;
image: string;
picture: string;
mfa: boolean;
groups: unknown[];
intercom_hash: string;
};
/** ISO8601形式 */
expires: string;
accessToken: string;
authProvider: string;
}